#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  //定义全局锁进行测试

//子线程
void* active(void* args)
{
    cout<<"I am a new thread..."<<endl;
    pthread_mutex_lock(&mutex);
    cout<<"I got a mutex!"<<endl;
    pthread_mutex_lock(&mutex);  //未释放继续加锁
    cout<<"I alive again!"<<endl;

}
//主线程
int main()
{ 
   //1.定义子线程标识符
   pthread_t td;
   //2.创建线程
    pthread_create(&td,nullptr,active,nullptr);
    sleep(1);
    cout<<"main thread running.."<<endl;
    sleep(3);  //主线程停顿3秒释放锁
    cout<<"main thread unlock..."<<endl;
    pthread_mutex_unlock(&mutex);
   //?.线程等待
    int n = pthread_join(td,nullptr); 
   return 0;   
}
